function [dfile, err] = read_one(truefile, RL, n, mode, subflag, avg)

global VOLTAGE WCHAN
global CONTROL DFILE

err=0;
dfile = [];
if(isempty(RL))
    err=2;
    return;
end;

watchon;

dfile=[];
CURRENT=[];
VOLTAGE=[];
WCHAN = [];
err=0;

readmode = getreadmode(nargin, n);
[m,smode] = getdatamode;
[p f e] = fileparts(truefile);
if(readmode)
    %	QueMessage(sprintf('Reading %s [%4d to %4d]', f, min(RL), max(RL)));
else
    QueMessage(sprintf('Manual Read: %12s mode=%s [%4d to %4d]', truefile, smode, RL(1), RL(length(RL))));
end
% if the averaging needs to be done, RL should only point to the first
% trace of a group; then the requested record list will be reconstructed...
% so that we can have discontinuous blocks brought together.
%
if(avg > 1) % averaging requested, so ask for all appropriate recordds
    RL2= [];
    for i = 1:length(RL)
        RL2 = [RL2 [RL(i):1:RL(i)+avg-1]];
    end;
else
    RL2 = RL;
end;

[dfile, data, err] = datac2mat(truefile, RL2);
switch(err) % handle errors - doesn't do much here because low level errors are handled in datac2mat.
    case 0 % is ok, just let it fall through
    otherwise
        watchoff;
end
if(isempty(data))
    err = 1;
    return;
end;

% check input args - if ctl is present, then use that information to override certain parameters
if(readmode)
    dfile.junctionpot = number_arg(CONTROL(n).jpot);
    dfile.vgain = number_arg(CONTROL(n).vgain);
    if(~subflag)
        dfile.igain = number_arg(CONTROL(n).igain);
        access = number_arg(CONTROL(n).access)/1000;
    else
        dfile.igain = number_arg(CONTROL(n).sub_igain);
        access = number_arg(CONTROL(n).sub_access)/1000;
    end
    dfile.wgain = number_arg(CONTROL(n).wgain);
    dfile.dmode = CONTROL(n).mode;
else % use some default values
    dfile.junctionpot = 0;
    % get these from the display window
    dfile.vgain = get_window_arg('Vgain', 10);
    dfile.igain = get_window_arg('Igain', 1);
    dfile.wgain = 1;
    if(dfile.mode < 5)
        dfile.dmode = smode; % for older files, use the window to set the data.
    end;
    access = 0;
end
DFILE = dfile;
% perform the scaling and corrections here
% KEY: data is reformatted from data into VOLTAGE, CURRENT and WCHAN in
% scaledat!!!!!
scaledat(data); % adjust scaling first: results are returned in VOLTAGE CURRENT and WCHAN
if(exist('avg') & avg > 1) % we need to do averaging, so call it and do it
    average(avg);
end;
if(dfile.mode == 6) % that's all for abf files.
    QueMessage('ABF File');
    watchoff;
    return;
end;

if(~readmode)
    watchoff;
    return;
end;


if(strcmp(mode, 'VC'))
    dv=max(max(VOLTAGE)) - min(min(VOLTAGE));
%    dfile.steps = find_steps(dfile, 'v', dv/200); 	% find the step positions in time
else
    dv=max(max(CURRENT)) - min(min(CURRENT));
%    dfile.steps = find_steps(dfile, 'i', dv);
end
% QueMessage(sprintf('Steps found: %d, delta command = %f', length(dfile.steps), dv));
cmsg = [sprintf('%d steps found', length(dfile.steps))];
if(strcmp(dfile.dmode,'VC') & readmode)
    [tra]=leak_corr(dfile, n); % in VOLTAGE clamp, correct the CURRENTs for leak
    if(number_arg(CONTROL(n).transwin) > 0)
        trans_correct(dfile, n);
    end
else
    if(strcmp(dfile.dmode, 'VC'))
        cmsg = [cmsg ', No transient corr'];
    end
end
if(strcmp(dfile.dmode, 'CC'))
    if(~isempty(CONTROL) & ~isempty(CONTROL(n)))
        idealize(dfile, CONTROL(n));
    end;

    if((number_arg(access) == -99.0/1000))
        t0 = floor(CONTROL(n).durho/(dfile.rate(1)*dfile.nr_channel(1)/1000))
        [r_uncomp] = autobridge(t0); % automated bridge arount t0 if access is -99...
        cmsg = [cmsg ', autobridge'];
    else
        [r_uncomp] = volt_corr(access, 0);
        cmsg = [cmsg sprintf(', Rs = %7.1f', access)];
    end;

else
    [r_uncomp] = volt_corr(access, 0); % correct the VOLTAGEs
    cmsg = [cmsg sprintf(', Rs = %7.1f', access)];
end;
QueMessage(cmsg);
watchoff;
return;


